package com.lyh.crm.workbench.web.controller;

import com.lyh.crm.settings.domain.User;
import com.lyh.crm.settings.service.UserService;
import com.lyh.crm.settings.service.impl.UserServiceImpl;
import com.lyh.crm.utils.DateTimeUtil;
import com.lyh.crm.utils.PrintJson;
import com.lyh.crm.utils.ServiceFactory;
import com.lyh.crm.utils.UUIDUtil;
import com.lyh.crm.vo.PaginationVO;
import com.lyh.crm.workbench.domain.Activity;
import com.lyh.crm.workbench.domain.Product;
import com.lyh.crm.workbench.service.ActivityService;
import com.lyh.crm.workbench.service.ProductService;
import com.lyh.crm.workbench.service.impl.ActivityServiceImpl;
import com.lyh.crm.workbench.service.impl.ProductServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ProductController extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        System.out.println("进入到产品控制器");

        String path = request.getServletPath();

        if("/workbench/product/save.do".equals(path)){

            save(request,response);

        }else if("/workbench/product/pageList.do".equals(path)){

            pageList(request,response);

        }else if("/workbench/product/delete.do".equals(path)){

            delete(request,response);

        }else if("/workbench/product/getProduct.do".equals(path)){

            getProduct(request,response);

        }else if("/workbench/product/update.do".equals(path)){

            update(request,response);

        }


    }

    private void update(HttpServletRequest request, HttpServletResponse response) {

        System.out.println("执行产品修改操作");

        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String unit = request.getParameter("unit");
        String cost = request.getParameter("cost");
        String price = request.getParameter("price");
        String specification = request.getParameter("specification");
        String category = request.getParameter("category");
        String introduce = request.getParameter("introduce");
        //修改时间：当前系统时间
        String editTime = DateTimeUtil.getSysTime();
        //修改人：当前登录用户
        String editBy = ((User)request.getSession().getAttribute("user")).getName();

        Product p = new Product();
        p.setId(id);
        p.setName(name);
        p.setUnit(unit);
        p.setCost(cost);
        p.setPrice(price);
        p.setSpecification(specification);
        p.setCategory(category);
        p.setIntroduce(introduce);
        p.setEditBy(editBy);
        p.setEditTime(editTime);

        ProductService ps = (ProductService) ServiceFactory.getService(new ProductServiceImpl());

        boolean flag = ps.update(p);

        PrintJson.printJsonFlag(response, flag);

    }

    private void getProduct(HttpServletRequest request, HttpServletResponse response) {

        System.out.println("进入到查询用户信息列表和根据产品id查询单条记录的操作");

        String id = request.getParameter("id");

        ProductService ps = (ProductService) ServiceFactory.getService(new ProductServiceImpl());

        Map<String,Object> map = ps.getProduct(id);

        PrintJson.printJsonObj(response, map);


    }

    private void delete(HttpServletRequest request, HttpServletResponse response) {

        System.out.println("执行产品的删除操作");

        String ids[] = request.getParameterValues("id");

        ProductService ps = (ProductService) ServiceFactory.getService(new ProductServiceImpl());

        boolean flag = ps.delete(ids);

        PrintJson.printJsonFlag(response, flag);


    }

    private void pageList(HttpServletRequest request, HttpServletResponse response) {

        System.out.println("进入到查询产品信息列表的操作（结合条件查询+分页查询）");


        String name = request.getParameter("name");
        String unit = request.getParameter("unit");
        String specification = request.getParameter("specification");
        String category = request.getParameter("category");
        String pageNoStr = request.getParameter("pageNo");
        int pageNo = Integer.valueOf(pageNoStr);
        //每页展现的记录数
        String pageSizeStr = request.getParameter("pageSize");
        int pageSize = Integer.valueOf(pageSizeStr);
        //计算出略过的记录数
        int skipCount = (pageNo-1)*pageSize;

        Map<String,Object> map = new HashMap<String,Object>();
        map.put("name", name);
        map.put("unit", unit);
        map.put("specification",specification);
        map.put("category",category);
        map.put("skipCount",skipCount);
        map.put("pageSize",pageSize);

        ProductService ps = (ProductService) ServiceFactory.getService(new ProductServiceImpl());

        PaginationVO<Product> vo = ps.pageList(map);

        //vo--> {"total":100,"dataList":[{市场活动1},{2},{3}]}
        PrintJson.printJsonObj(response, vo);




    }

    private void save(HttpServletRequest request, HttpServletResponse response) {

        System.out.println("执行产品添加操作");

        String id = UUIDUtil.getUUID();

        String name = request.getParameter("name");
        String unit = request.getParameter("unit");
        String cost = request.getParameter("cost");
        String price = request.getParameter("price");
        String specification = request.getParameter("specification");
        String category = request.getParameter("category");
        String introduce = request.getParameter("introduce");
        //创建时间：当前系统时间
        String createTime = DateTimeUtil.getSysTime();
        //创建人：当前登录用户
        String createBy = ((User)request.getSession().getAttribute("user")).getName();

        Product p = new Product();
        p.setId(id);
        p.setName(name);
        p.setUnit(unit);
        p.setCost(cost);
        p.setPrice(price);
        p.setSpecification(specification);
        p.setCategory(category);
        p.setIntroduce(introduce);
        p.setCreateTime(createTime);
        p.setCreateBy(createBy);

        ProductService ps = (ProductService) ServiceFactory.getService(new ProductServiceImpl());

        boolean flag = ps.save(p);

        PrintJson.printJsonFlag(response, flag);

    }

}
